package com.it.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.dao.BlogCommentMapper;
import com.it.domain.BlogCommentEntity;
import com.it.service.BlogCommentService;
import org.springframework.beans.BeanUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Service
public class BlogCommentServceImpl extends ServiceImpl<BlogCommentMapper, BlogCommentEntity> implements BlogCommentService {

    /**
     * to:创建时间排序，将获取到的评论进行处理，分级显示
     */
    @Override
    public List<BlogCommentEntity> listCommentByBlogId(Long blogId) {
        //找到一级评论
        List<BlogCommentEntity> rootBlogCommentEntity = this.baseMapper.selectList(new QueryWrapper<BlogCommentEntity>().eq("parent_comment_id", "0"));
        return eachComment(rootBlogCommentEntity);
    }

    @Transactional
    @Override
    public BlogCommentEntity saveComment(BlogCommentEntity blogCommentEntity) {

        Long parentCommentId = blogCommentEntity.getParentBlogCommentEntity().getId();
        if (parentCommentId != -1) {
            //说明是回复某个评论
            blogCommentEntity.setParentBlogCommentEntity(this.baseMapper.selectById(parentCommentId));
        } else {
            blogCommentEntity.setParentBlogCommentEntity(null);
        }
        blogCommentEntity.setCreateTime(new Date());
        this.baseMapper.insert(blogCommentEntity);
        return blogCommentEntity;
    }

    /**
     * 循环每一个顶层节点
     *
     * @param blogCommentEntities:顶级节点
     * @return ：顶级节点的及其子集合
     */
    private List<BlogCommentEntity> eachComment(List<BlogCommentEntity> blogCommentEntities) {
        //创建一个集合对集合进行存储，防止修改对象的属性造成整个集合的修改
        List<BlogCommentEntity> commentsView = new ArrayList<>();
        for (BlogCommentEntity blogCommentEntity : blogCommentEntities) {
            BlogCommentEntity c = new BlogCommentEntity();
            BeanUtils.copyProperties(blogCommentEntity, c);
            commentsView.add(c);
        }
        /*合并各层自带到第一级的子代集合中*/
        combineChildren(commentsView);
        return commentsView;
    }

    /**
     * @param blogCommentEntities :root根节点，blog不为空的对象集合
     */
    private void combineChildren(List<BlogCommentEntity> blogCommentEntities) {
        for (BlogCommentEntity blogCommentEntity : blogCommentEntities) {
            List<BlogCommentEntity> replyBlogCommentEntities = blogCommentEntity.getReplyBlogCommentEntities();
            for (BlogCommentEntity replyBlogCommentEntity : replyBlogCommentEntities) {
                recursively(replyBlogCommentEntity);
            }
            //将类的公共结构存放到自己的引用中去
            blogCommentEntity.setReplyBlogCommentEntities(tempReplys);
            tempReplys = new ArrayList<>();
        }
    }

    /**
     * 存放子集的容器
     */
    private List<BlogCommentEntity> tempReplys = new ArrayList<>();

    private void recursively(BlogCommentEntity blogCommentEntity) {
        tempReplys.add(blogCommentEntity);
        if (blogCommentEntity.getReplyBlogCommentEntities().size() > 0) {
            List<BlogCommentEntity> replyBlogCommentEntities = blogCommentEntity.getReplyBlogCommentEntities();
            for (BlogCommentEntity replyBlogCommentEntity : replyBlogCommentEntities) {
                tempReplys.add(replyBlogCommentEntity);
                if (replyBlogCommentEntity.getReplyBlogCommentEntities().size() > 0) {
                    recursively(replyBlogCommentEntity);
                }
            }
        }
    }


}
